package org.example.aigc.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.example.aigc.entity.Appearance;
import org.example.aigc.entity.Audio;
import org.example.aigc.entity.Course;
import org.example.aigc.entity.Student;
import org.example.aigc.service.UserService;
import org.example.aigc.utils.Reply;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/user")
@RestController
public class UserController {

    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 测试 API，获取所有学生的数据
     *
     * @return 返回所有学生的数据
     */
    @GetMapping("/getStudent")
    public Reply getAllStudent() {
        List<Student> students = userService.getAllStudent();
        return new Reply("获取学生数据").ok(0, "成功").put("students", students);
    }

    /**
     * 用户信息注册 API，在数据库中添加用户信息
     *
     * @param request  请求变量
     * @param response 回复变量
     * @param username  用户名
     * @param password  密码
     * @param isStudent 是否为学生
     * @return 返回注册的状态
     */
    @PostMapping("/register")
    public Reply register(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam String username,
            @RequestParam String password,
            @RequestParam Boolean isStudent
    ) {
        int code = userService.hasName(username);
        if (code == 1) {
            return new Reply("用户信息注册").error(1, "用户名已被注册");
        }
        code = userService.register(request, response, username, password, isStudent);
        return code == 0 ?
                new Reply("用户信息注册").ok(0, "成功") :
                new Reply("用户信息注册").error(2, "其他错误");
    }


    /**
     * 用户登录 API
     *
     * @param request  请求变量
     * @param response 回复变量
     * @param username 用户名
     * @param password 密码
     * @return 返回登录状态以及登录身份
     */
    @PostMapping("/login")
    public Reply login(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam String username,
            @RequestParam String password
    ) {
        return userService.login(request, response, username, password);
    }

    /**
     * 用户收藏课程
     *
     * @param request  请求变量
     * @param response 回复变量
     * @param courseId 课程 id
     * @return 返回收藏课程的执行状态
     */
    @PostMapping("/favorite")
    public Reply favorite(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam Long courseId
    ) {
        int code = userService.favorite(request, response, courseId);
        switch (code) {
            case 0 -> {
                return new Reply("用户收藏课程").ok(0, "成功");
            }
            case 1 -> {
                return new Reply("用户收藏课程").error(1, "您尚未登录");
            }
            case 2 -> {
                return new Reply("用户收藏课程").error(2, "您已收藏该课程");
            }
            default -> {
                return new Reply("用户收藏课程").error(3, "其他错误");
            }
        }
    }

    /**
     * 用户取消收藏课程
     *
     * @param request  请求变量
     * @param response 回复变量
     * @param courseId 课程 id
     * @return 返回取消课程的执行状态
     */
    @PostMapping("/unFavorite")
    public Reply unFavorite(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam Long courseId
    ) {
        int code = userService.unFavorite(request, response, courseId);
        switch (code) {
            case 0 -> {
                return new Reply("用户取消收藏课程").ok(0, "成功");
            }
            case 1 -> {
                return new Reply("用户取消收藏课程").error(1, "您尚未登录");
            }
            case 2 -> {
                return new Reply("用户取消收藏课程").error(2, "您未收藏该课程");
            }
            default -> {
                return new Reply("用户收藏课程").error(3, "其他错误");
            }

        }
    }

    /**
     * 查询用户收藏的课程
     *
     * @param request  请求变量
     * @param response 回复变量
     * @return 查询状态和收藏列表
     */
    @GetMapping("/getFavorite")
    public Reply getFavorite(
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        List<Course> courses = userService.getFavorite(request, response);
        return courses == null ?
                new Reply("查询用户收藏课程").error(1, "您尚未登录") :
                new Reply("查询用户收藏课程").ok(0, "成功").put("courses", courses);

    }

    /**
     * 查询教师上传的课程
     *
     * @param request  请求变量
     * @param response 回复变量
     * @return 返回查询状态和课程列表
     */
    @GetMapping("/getUploaded")
    public Reply getUploaded(
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        return userService.getUploaded(request, response);
    }

    /**
     * 创建声音模型
     *
     * @param request    请求变量
     * @param audioName  声音文件名
     * @param audioLabel 声音模型标签
     * @return 创建的声音 id
     */
    @PostMapping("/uploadAudio")
    public Reply uploadAudio(
            HttpServletRequest request,
            @RequestParam("audioName") String audioName,
            @RequestParam("audioLabel") String audioLabel
    ) {
        return userService.uploadAudio(request, audioName, audioLabel);
    }

    /**
     * 获取用户的声音模型列表
     *
     * @param request  请求变量
     * @param response 回复变量
     * @return 用户声音模型列表
     */
    @GetMapping("/getAudio")
    public Reply getAudio(
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        List<Audio> audios = userService.getAudio(request, response);
        return audios == null ?
                new Reply("查询用户收藏音频").error(1, "您没有权限") :
                new Reply("查询用户收藏音频").ok(0, "成功").put("audios", audios);
    }

    /**
     * 根据 audio_id 获取 audio 信息
     *
     * @param request  请求变量
     * @param response 回复变量
     * @param audioId  audio的id
     * @return 返回 audio 信息
     */
    @PostMapping("/getAudioById")
    public Reply getAudioById(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam("audioId") Long audioId
    ) {
        Audio audio = userService.getAudioById(request, response, audioId);
        return audio == null ?
                new Reply("获取音频 " + audioId.toString()).error(1, "您没有权限") :
                new Reply("获取音频 " + audioId.toString()).ok(0, "成功").put("audio", audio);
    }

    /**
     * 根据 appearance_id 获取 appearance 信息
     *
     * @param request      请求变量
     * @param response     回复变量
     * @param appearanceId appearance的id
     * @return 返回 appearance 信息
     */
    @PostMapping("/getAppearanceById")
    public Reply getAppearanceById(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam("appearanceId") Long appearanceId
    ) {
        Appearance appearance = userService.getAppearanceById(request, response, appearanceId);
        return appearance == null ?
                new Reply("获取虚拟形象 " + appearanceId.toString()).error(1, "您没有权限") :
                new Reply("获取虚拟形象 " + appearanceId.toString()).ok(0, "成功").put("appearance", appearance);
    }

    /**
     * 删除音频
     *
     * @param id 待删除的音频id
     * @return 删除状态
     */
    @PostMapping("/deleteAudio")
    public Reply deleteAudio(
            @RequestParam("id") Long id) {
        int code = userService.deleteAudio(id);
        switch (code) {
            case 0 -> {
                return new Reply("删除音频 " + id).ok(code, "成功");
            }
            case 1 -> {
                return new Reply("删除音频" + id).error(code, "未查找到此音频");
            }
            case 2 -> {
                return new Reply("删除音频 " + id).error(code, "音频不存在");
            }
            case 3 -> {
                return new Reply("删除音频 " + id).error(code, "删除音频失败");
            }
            default -> {
                return new Reply("删除音频 " + id).error(4, "其他错误");
            }
        }
    }
    /**
     * 创建外形模型
     *
     * @param request    请求变量
     * @param appearanceName  外形文件名
     * @param appearanceLabel 外形标签
     * @return 创建的外形 id
     */
    @PostMapping("/uploadAppearance")
    public Reply uploadAppearance(
            HttpServletRequest request,
            @RequestParam("appearanceName") String appearanceName,
            @RequestParam("appearanceLabel") String appearanceLabel,
            @RequestParam("coverName") String coverName
    ) {
        return userService.uploadAppearance(request, appearanceName, appearanceLabel, coverName);
    }

    /**
     * 获取用户的外形列表
     *
     * @param request  请求变量
     * @param response 回复变量
     * @return 查询状态和外形列表
     */
    @GetMapping("/getAppearance")
    public Reply getAppearance(
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        List<Appearance> appearances = userService.getAppearance(request, response);
        return appearances == null ?
                new Reply("查询用户外形").error(1, "您没有权限") :
                new Reply("查询用户外形").ok(0, "成功").put("appearances", appearances);
    }

    /**
     * 删除外形
     *
     * @param id 待删除的外形id
     * @return 删除状态
     */
    @PostMapping("/deleteAppearance")
    public Reply deleteAppearance(
            @RequestParam("id") Long id) {
        int code = userService.deleteAppearance(id);
        switch (code) {
            case 0 -> {
                return new Reply("删除外形 " + id).ok(code, "成功");
            }
            case 1 -> {
                return new Reply("删除外形" + id).error(code, "未查找到此外形");
            }
            case 2 -> {
                return new Reply("删除外形 " + id).error(code, "外形不存在");
            }
            case 3 -> {
                return new Reply("删除外形 " + id).error(code, "删除外形失败");
            }
            default -> {
                return new Reply("删除外形 " + id).error(3, "其他错误");
            }
        }
    }
}
